<?php 

   /**
    *
    * FILE:          cieListQuery.php
    * LOCATION:      /root
    * REFERENCES:    admin.php
    *                cieSearch.php
    *                cieEdit.php
    * FUNCTION:      displays a table of consumers that are
    *                returned from the database as a resultset
    *                based on search parameters from cieSearch.php
    * DEPENDENCIES:  db_connect.php 
    * 
    * --> CREATED 10-18-07
    * ==============================================================================
    * 
    * @version $Id: admin.php,v 0.1 2007/09/3
    * @copyright Copyright (c) none
    * @author Peter Holcomb
    * @license http://opensource.org/licenses/gpl-license.php GNU General 
    * Public License Version 2 (GPL v2)
    * 
    * ==============================================================================
    */
    
        //connect to database
	include('includes/db_connect.php');
	include_once('./classes/pagination.class.php');
	
	//what were the search parameters sent by user?
	$searchStr  = $_POST['searchStr'];
	$searchAttr = $_POST['searchAttr'];
	


	//echo "Search String: ".$searchStr." Search Attribute: ".$searchAttr."<br/>";

       

        //set query request to match correct database field
	if($searchAttr=='lName') {

                //query by last name
		$col = 'c.last_name';

	} else if($searchAttr=='fName') {

                //query by first name
		$col = 'c.first_name';

	} else if($searchAttr=='city') {

                //query by city
		$col = 'c.city';

	} else if($searchAttr=='phone') {

                //query by phone number
		$col = 'c.phone';

	}else if($searchAttr=='false') {

                //no query option chosen
		$col = null;
	}

        //pagination

        //main pagination settings
	$items = 10;
	$page = 1;

        if(isset($_GET['page']) and is_numeric($_GET['page']) and $page = $_GET['page']){

		$limit = " LIMIT ".(($page-1)*$items).",$items";

         }else{

		$limit = " LIMIT $items";
         }


	if($col != null) {

                 $sqlStr = "SELECT c.id, 
                                   c.last_name, 
                                   c.first_name, 
                                   c.city, 
                                   c.phone, 
                                   c.address, 
                                   c.zip, 
                                   c.phone, 
                                   ls.name as state 
		            FROM       consumer c, 
                                       lst_state ls 
                            WHERE  ".$col." 
                            LIKE \"%".$searchStr."%\" 
                            and    c.state_id = ls.id 
                            order by c.last_name asc";

	         //$sqlStrAux = "SELECT count(*) as total FROM consumer";
                 //$aux = Mysql_Fetch_Assoc(mysql_query($sqlStrAux));

	         $query = mysql_query($sqlStr.$limit);
 
                 $total_returned = mysql_num_rows($query);


                 if($total_returned < 1){
?>
                    
                     <center><h2>There were no results that matched your search request.</h2></center>

                     <!-- USER HAS OPTION TO CREATE NEW CONSUMER CASE FILE -->
                     <p style="text-align:right;">
	                 <a style="border-top:1px #ccc dotted" href="admin.php?action=cieedit&id=0"><strong>Create New Case File</strong></a>
                     </p>
<?php
                      exit();
                 }


                 //let the user know search results were found
                 echo "<span id=\"records_indicator\">".$total_returned."</span> 
                     <h2>Consumer case files matched your search request.</h2>";


                 //style counter
	         $i  = 0;

	        if($total_returned >0){
			$p = new pagination;
			$p->Items($aux['total']);
			$p->limit($items);
			$p->target("admin.php?action=cielistall");
			$p->currentPage($page);
			$p->calculate();
			$p->changeClass("pagination");
?>


                        <!-- THIS TABLE EDITED ON 10-3-07 TO RENDER NEW TABLE STYLE -->
                        <!-- SET UP RESULTSET TABLE -->
                        <table class="table1">
                           <thead> 
                             <tr>
                                <th>Full Name</th>
                                <th>Address</th>
                                <th>City</th>
                                <th>State</th>
                                <th>Zip</th>
                                <th>Phone</th>
                             </tr>
                           </thead>
                           <tbody>

<?php 

                        /*
                           we want to alternate the style of each row, so set 
                           style attribute to odd or even based on query resultset 
                           given by database
                        */
   
                        while($row=mysql_fetch_array($query)) { 
	  
                              if($i%2==0) {
		 
                                     echo "<tr class='even'>";
	
                              }else {
	    
                                     echo "<tr class='odd'>";
	      
                              }
?>
      
                             <!-- USER CAN CLICK ON FIRST OR LAST NAME TO VIEW CONSUMER CASE FILE INFO -->
                             <!-- changed to fullname on 10-3-07 -->
                             <td>
                                <a href="admin.php?action=cieedit&id=<?php echo $row["id"]; ?>">
                                    <?php echo $row["last_name"].", ".$row["first_name"]; ?>
                                </a>
                             </td>
                             <td><?php echo $row["address"]; ?></td>
                             <td><?php echo $row["city"]; ?></td>
                             <td><?php echo $row["state"]; ?></td>
                             <td><?php echo $row["zip"]; ?></td>
                             <td><?php echo $row["phone"]; ?></td>
                          </tr>
                     </tbody>
  
<?php 

                         $i++; 
      
                     }//end result set while loop 
   
?>
                   </table>
                   <!-- END RESULTSET TABLE -->

<?php 
                   $p->show();

               } else { 
    
?>

                    <center><h1>Sorry, no results were found</h1></center>


<?php 

               }

         } else {
	         
?>

      <center><h1>Sorry, no results were found</h1></center>


<?php 

   } 
   
?>



   <br/>

<!-- USER HAS OPTION TO CREATE NEW CONSUMER CASE FILE -->
<p style="text-align:right;">
	<a style="border-top:1px #ccc dotted" href="admin.php?action=cieedit&id=0"><strong>Create New Case File</strong></a>
</p>